<?php
require_once("db_functions.php");
require_once("page.class.php");

define("PAGE_LENGTH",20);
define("PAGE_ITEMS",5);
define("PAGE_SKIP",10);

class geos extends page{
	var $input;
	var $db;
	
	var $tipovi_lokacija = array(
		1 => "grad",
		2 => "lok.opština",
		3 => "naselje"
	);
	
	function geos($get,$post){
		$this->db = new db_functions();
		$this->input = array_merge($get,$post);
		
		$action = getFromInput("a");
		switch ($action){
			case "regioni":
				$this->regioni();
				break;
			case "opstine":
				$this->opstine();
				break;
			case "lokacije":
				$this->lokacije();
				break;
			case "ulice":
				$this->ulice();
				break;
			case "ulice_lokacije":
				$mod = getFromInput("mod");
				switch ($mod){
					case "new":
						$this->ulice_lokacije_new();
						break;
					case "delete":
						$this->ulice_lokacije_delete();
						break;
					default:
						$this->ulice_lokacije();
						break;
				}
				break;
		}
	}
	
	function regioni(){
		$mod = getFromInput("mod");
		switch ($mod){
			case "save":
				$newname = readValFromInput("naziv");
				$regid = getFromInput("id");
				$q = "UPDATE regioni SET region_name='$newname' WHERE region_id=$regid LIMIT 1";
				if($this->db->alterTable($q)){
					echo "Naziv regiona izmenjen.";
				}else{
					echo mysql_error();
				}
				break;
			default:
				$this->regioniAdmin();
				break;
		}
	}
	function regioniAdmin(){
		$q = "SELECT * FROM regioni";
		
		$fname = trim(readValFromInput("filter_name"));
		if(strlen($fname)){
			$this->assign("filter_name",$fname);
			if (preg_match('/[^\d]/',$fname)) {
				$q .= " WHERE region_name LIKE '%$fname%'";
			}else{
				$q .= " WHERE region_id=$fname";
			}
		}
		$q .= " ORDER BY region_name ASC";
		
		$res = $this->db->select2($q);
		if($res !== false){
			$this->assign("info",count($res)." regiona");
			foreach ($res as $row){
				$ids[] = $row["region_id"];
				$names[] = $row["region_name"];
			}
			$this->assign("regid",$ids);
			$this->assign("regname",$names);
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->No results");
		}
		
		echo $this->parseTemplate("geos/regioni.tpl");
	}
	
	function opstine(){
		$mod = getFromInput("mod");
		switch ($mod){
			case "save":
				$opsid = $this->input["edit_opsid"];
				$opsnaz = readValFromInput("edit_opsname");
				$regid = $this->input["edit_region"];
				
				if($this->db->alterTable("UPDATE opstine SET opstina_name='$opsnaz', opstina_region_id=$regid WHERE opstina_id=$opsid LIMIT 1")){
					echo "Opština izmenjena.";
				}else{
					echo mysql_error();
				}
				break;
			default:
				$this->opstineAdmin();
				break;
		}
	}
	function opstineAdmin(){
		
		$page = getFromInput("page");
		$page = $page>0?$page:1;
		
		$fregid = getFromInput("filter_region");
		$polja = $this->get_Regioni($fregid);
			$this->assign("rid",$polja["polje_id"]);
			$this->assign("rname",$polja["polje_ime"]);
			$this->assign("selected_reg",$polja["polje_selected"]);
		
		$q = "SELECT opstine.*, region_name FROM opstine, regioni WHERE opstine.opstina_region_id=region_id";
		
		$fname = trim(readValFromInput("filter_name"));
		$freg  = trim(getFromInput("filter_region"));
		if(strlen($fname)){
			$this->assign("filter_name",$fname);
			if (preg_match('/[^\d]/',$fname)) {
				$q .= " AND opstina_name LIKE '%$fname%'";
			}else{
				$q .= " AND opstina_id=$fname";
			}
		}
		if($freg>0){
			$q .= " AND opstina_region_id=$freg";
		}
		
		$resall = $this->db->select1($q);
		
		if($resall !== false){
			$numall = count($resall);
			$this->assign("info","$numall opštine");
			$this->assign("current_page",$page);
			
			$q .= " ORDER BY opstina_name ASC";
			
			$numpages = bcdiv($numall,PAGE_LENGTH,0);
			if($numall % PAGE_LENGTH >0){
				$numpages++;
			}
			if($numpages>1){
				for ($i=1; $i<=$numpages; $i++){
					$pages[] = $i;
					if($i==$page){
						$cur_page[] = "current";
					}else{
						$cur_page[] = "";
					}
				}
			}else{
				$pages = array(1);
				$cur_page = array("current");
			}
			$this->assign("page",$pages);
			$this->assign("curr_page",$cur_page);
			
			
			
			$start = ($page-1)*PAGE_LENGTH;
			$q .= " LIMIT $start,".PAGE_LENGTH;
			
			$res = $this->db->select2($q);
			if($res === false){
				$q = substr($q,0,stripos($q,"limit")+6)." 0,".PAGE_LENGTH;
				$res = $this->db->select2($q);
			}		
			
			foreach ($res as $row){
				$ids[] = $row["opstina_id"];
				$names[] = $row["opstina_name"];
				$regids[] = $row["opstina_region_id"];
				$regs[] = $row["region_name"];
			}
			$this->assign("opstina_id",$ids);
			$this->assign("opstina_name",$names);
			$this->assign("region_id",$regids);
			$this->assign("region",$regs);
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->No results");
		}
		
		echo $this->parseTemplate("geos/opstine.tpl");
	}
	
	function lokacije(){
		$mod = getFromInput("mod");
		switch ($mod){
			case "getParents":
				$this->getLokParents();
				break;
			case "save":
				$this->saveLokacija();
				break;
			case "saveNew":
				$this->saveNewLokacija();
				break;
			default:
				$this->lokacijeAdmin();
				break;
		}
	}
	function lokacijeAdmin(){
		$page = getFromInput("page");
		$page = $page>0?$page:1;
		$this->assign("pageID",$page);
		
		$userLevel = $this->getUserLevel();
		if (!preg_match('/^admin$/',$userLevel["name"])) {
			$this->assign("comment_admin_start","<!--");
			$this->assign("comment_admin_end","-->");
		}
		
		//print_rarray($_POST);
		
		// FILTER TIP LOKACIJE
		$tip = getFromInput("filter_tip");
		$tipsel = array("","","");
		if($tip>0){
			$tipsel[$tip-1] = " selected";
		}
		switch ($tip){
			case "1":
				$this->assign("fpg_display","display:block; ");
				$this->assign("fplo_display","display:none; ");
				$this->assign("fpn_display","display:none; ");
				break;
			case "2":
				$this->assign("fpg_display","display:none; ");
				$this->assign("fplo_display","display:block; ");
				$this->assign("fpn_display","display:none; ");
				break;
			case "3":
				$this->assign("fpg_display","display:none; ");
				$this->assign("fplo_display","display:none; ");
				$this->assign("fpn_display","display:block; ");
				break;
			default:
				$this->assign("fpg_display","display:none; ");
				$this->assign("fplo_display","display:none; ");
				$this->assign("fpn_display","display:none; ");
				break;
		}
		
		
		$this->assign("tipid",array(1,2,3));
		$this->assign("tipname",array("grad","lok.opština","naselje"));
		$this->assign("selected_tip",$tipsel);
		
		// FILTER LOKACIJE PARENT
		$polja = $this->get_Opstine(getFromInput("filter_parent_grad"));
			$this->assign("fpg_id",$polja["polje_id"]);
			$this->assign("fpg_name",$polja["polje_ime"]);
			$this->assign("fpg_selected",$polja["polje_selected"]);
		$polja = $this->get_Lokacije(1,getFromInput("filter_parent_lokopstina"));
			$this->assign("fplo_id",$polja["polje_id"]);
			$this->assign("fplo_name",$polja["polje_ime"]);
			$this->assign("fplo_selected",$polja["polje_selected"]);
		$polja = $this->get_Lokacije(2,getFromInput("filter_parent_naselje"));
			$this->assign("fpn_id",$polja["polje_id"]);
			$this->assign("fpn_name",$polja["polje_ime"]);
			$this->assign("fpn_selected",$polja["polje_selected"]);
			
		$q = "SELECT * FROM lokacije WHERE 1";
		
		$filter_name = trim(readValFromInput("filter_name"));
		if(strlen($filter_name)){
			if (preg_match('/[^\d]/',$filter_name)) {
				$q .= " AND lok_naziv LIKE '%$filter_name%'";
			}else{
				$q .= " AND lid=$filter_name";
			}
			
			$this->assign("filter_name",$filter_name);
		}
		if($tip>0){
			$q .= " AND lok_tip=$tip";
			switch ($tip){
				case 1:
					$parent = getFromInput("filter_parent_grad");
					break;
				case 2:
					$parent = getFromInput("filter_parent_lokopstina");
					break;
				case 3:
					$parent = getFromInput("filter_parent_naselje");
					break;
				default:
					$parent = false;
			}
			if($parent>0){
				$q .= " AND lok_parent=$parent";
			}
		}
		$res_all = $this->db->select2($q);
		
		if($res_all !== false){
			$numall = count($res_all);
			
			$start = ($page-1)*PAGE_LENGTH;
			$q .= " ORDER BY lok_naziv LIMIT $start,".PAGE_LENGTH;
			$res = $this->db->select2($q);
			$this->assign("info",count($res_all)." lokacija");
			
			//echo $q;
			//print_rarray($res_all);
			
			$numpages = bcdiv($numall,PAGE_LENGTH,0);
			if(($numall % PAGE_LENGTH)>0){
				$numpages++;
			}
			if($numpages>0){
				for ($i=1; $i<=$numpages; $i++){
					$pages[] = $i;
					if($i == $page){
						$cur_page[] = "current";
					}else{
						$cur_page[] = "";
					}
				}
				$this->assign("page",$pages);
				$this->assign("current_page",$cur_page);
			}
			if($res === false){
				$q = substr($q,0,stripos($q," limit"));
				$res = $this->db->select2($q);
			}
			
			foreach ($res as $row){
				$ids[] = $row["lid"];
				$ptts[] = $row["lok_ptt"];
				$names[] = $row["lok_naziv"];
				$type_ids[] = $row["lok_tip"];
				$types[] = $this->tipovi_lokacija[$row["lok_tip"]];
				$parents[] = $this->get_Parent($row["lok_tip"],$row["lok_parent"]);
				$parent_ids[] = $row["lok_parent"];
			}
			//print_rarray($res);
			$this->assign("lid",$ids);
			$this->assign("lok_tip_id",$type_ids);
			$this->assign("lok_tip",$types);
			$this->assign("lok_ptt",$ptts);
			$this->assign("lok_naziv",$names);
			$this->assign("lok_parent",$parents);
			$this->assign("lok_parent_id",$parent_ids);
			
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->No results");
		}
		
		
		echo $this->parseTemplate("geos/lokacije.tpl");
	}
	function saveLokacija(){
		$lid = getFromInput("lid");
		$ptt = trim(getFromInput("lok_ptt"));
		$name = readValFromInput("lok_name");
		$tip = getFromInput("lok_tip");
		
		$oldType = getFromInput("oldType");
		$oldParent = getFromInput("oldParent");
		
		switch ($tip){
			case "1":
				$parent = getFromInput("lok_grad_parent");
				break;
			case "2":
				$parent = getFromInput("lok_lokops_parent");
				break;
			case "3":
				$parent = getFromInput("lok_naselje_parent");
				break;
		}
		if (!preg_match('/^\d{5}$/',$ptt) && strlen($ptt)>0) {
			die("GREŠKA!\nPTT broj nije validan!");
		}
		
		if(($parent==$lid) && (intval($tip)==($oldType+1))){
			die("Greška!\nZa roditelja lokacije ste postavili lokaciju koju trenutno menjate,\nšto nije moguće.");
		}
		
		$q = "UPDATE lokacije SET lok_ptt='$ptt', lok_naziv='$name', lok_tip=$tip, lok_parent=$parent WHERE lid=$lid";
		//echo $q;die();
		if($this->db->alterTable($q)){
			echo "Lokacija izmenjena";
		}else{
			echo mysql_error();
		}
	}
	function saveNewLokacija(){
		$ptt = trim(getFromInput("new_lok_ptt"));
		$name = trim(readValFromInput("new_lok_name"));
		$type = getFromInput("new_lok_tip");
		$parent = getFromInput("new_parent");
		
		if (!preg_match('/^\d{5}$/',$ptt) && strlen($ptt)>0) {
			die("PTT broj nije validan!");
		}
		
		if (strlen($name)==0) {
			die("Niste uneli naziv lokacije!");
		}
		
		$ima = $this->db->getOne("SELECT 1 FROM lokacije WHERE lok_naziv='$name' AND lok_tip=$type AND lok_parent=$parent LIMIT 1");
		if ($ima>0){
			die("Lokacija pod tim nazivom, tog tipa i u toj oblasti već postoji!");
		}
		
		$q = "INSERT INTO lokacije(lok_ptt,lok_naziv,lok_parent,lok_tip) VALUES('$ptt','$name',$parent,$type)";
		if ($this->db->alterTable($q)) {
			echo "Lokacija sačuvana";
		}else{
			echo mysql_error();
		}
		
	}
	
	function ulice(){
		switch ($this->input["mod"]){
			case "add":
				$name = readValFromInput("u");
				$q = "SELECT ulica_id FROM ulice WHERE ulica_ime='$name'";
				$ima = $this->db->getOne($q);
				if($ima){
					echo "Ulica '$name' već postoji u bazi i ima šifru: $ima";
				}else{
					if($this->db->alterTable("INSERT INTO ulice(ulica_ime) VALUES('$name')")){
						echo "Ulica je dodata.";
					}else{
						echo mysql_error();
					}
				}
				break;
			case "save":
				$ulica	= readValFromInput("ul");
				$ulid	= readValFromInput("id");
				
				$q = "UPDATE ulice SET ulica_ime='$ulica' WHERE ulica_id=$ulid";
				if($this->db->alterTable($q)){
					echo "Naziv ulice promenjen.";
				}else{
					echo mysql_error();
				}
				break;
			case "view":
			default:
				$this->uliceAdmin();
				break;
		}
	}	
	function uliceAdmin(){
		if(!($page=$this->input["page"])){
			$page = 1;
		}
		$start = ($page-1)*PAGE_LENGTH;
		$end = $start+PAGE_LENGTH;
		
		$kword_ulica = trim($this->input["ul"]);
		
		$q = "SELECT * FROM ulice ";
				
		if(strlen($kword_ulica)>0){
			if(preg_match('/[^\d]/',$kword_ulica)){
				$q .= "WHERE ulica_ime LIKE '%$kword_ulica%' ";
			}else{
				$q .= "WHERE ulica_id=$kword_ulica ";
			}
			$this->assign("filter_ulica",$kword_ulica);
		}
		
		$q .= "ORDER BY ulica_ime ";
		$res = $this->db->select1($q);
		if($res[0]){
			$all = count($res);
			$this->assign("info","$all ulica");
			if($all > PAGE_LENGTH){
				$num_pages = bcdiv($all,PAGE_LENGTH,0);
				if($all%PAGE_LENGTH>0){
					$num_pages++;
				}
				
				if($num_pages>((2*PAGE_ITEMS)+1)){
					if($page<PAGE_ITEMS+1){
						$start_pf = 1;
						$end_pf = 2*PAGE_ITEMS+1;
					}else{
						if($page>($num_pages-PAGE_ITEMS)){
							$start_pf = $num_pages-(2*PAGE_ITEMS)-1;
							$end_pf = $num_pages;
						}else{
							$start_pf = $page-5;
							$end_pf = $page+5;
						}
					}
					//echo "$start_pf-$end_pf";
					
					for ($i=$start_pf; $i<=$end_pf; $i++){
						$pages[] = $i;
						if($i == $page){
							$curr[] = "current";
						}else{
							$curr[] = "";
						}
					}
				}else{
					for ($i=1; $i<=$num_pages; $i++){
						$pages[] = $i;
						if($i == $page){
							$curr[] = "current";
						}else{
							$curr[] = "";
						}
					}
				}
				$this->assign("page",$pages);
				$this->assign("current_page",$curr);
			}
		}
		$q .= "LIMIT $start,".PAGE_LENGTH;
		//echo $q;
		$res = $this->db->select2($q);
		if($res[0]){
			foreach ($res as $ulica){
				$ids[] = $ulica["ulica_id"];
				$names[] = $ulica["ulica_ime"];
			}
			$this->assign("ulid",$ids);
			$this->assign("ulname",$names);
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->");
			$this->assign("message","No results");
		}
		$this->assign("first_page",1);
		$this->assign("last_page",$num_pages);
		$this->assign("page_num",$page);
		$this->assign("skip_pages",PAGE_SKIP);
		
		
		echo $this->parseTemplate("geos/ulice.tpl");
	}
	
	function ulice_lokacije(){
		$page = getFromInput("page");
		
		$fUlica = readValFromInput("filter_ulica");
		$this->assign("filter_ulica",$fUlica);
		
		$fGradLid = getFromInput("filter_grad");
		$gradovi = $this->get_Lokacije("1",$fGradLid);
			$this->assign("grad_id",$gradovi["polje_id"]);
			$this->assign("grad_name",$gradovi["polje_ime"]);
			$this->assign("selected_grad",$gradovi["polje_selected"]);
			$this->assign("grad_ptt",$gradovi["polje_ptt"]);
		$fLokopsLid = getFromInput("filter_lokops");
		$lokops = $this->get_Lokacije("2",$fLokopsLid);
			$this->assign("lokops_id",$lokops["polje_id"]);
			$this->assign("lokops_name",$lokops["polje_ime"]);
			$this->assign("selected_lokops",$lokops["polje_selected"]);
			$this->assign("lokops_ptt",$lokops["polje_ptt"]);
		$fNaseljeLid = getFromInput("filter_naselje");
		$naselja = $this->get_Lokacije("3",$fNaseljeLid);
			$this->assign("naselje_id",$naselja["polje_id"]);
			$this->assign("naselje_name",$naselja["polje_ime"]);
			$this->assign("selected_naselje",$naselja["polje_selected"]);
			$this->assign("gnaselje_ptt",$naselja["polje_ptt"]);
		
		$fLokTip = getFromInput("filter_tip_lok");
		$polja = $this->get_LokacijeTipovi($fLokTip);
			$this->assign("tip_id",$polja["polje_id"]);
			$this->assign("tip_name",$polja["polje_ime"]);
			$this->assign("selected_filter_tip",$polja["polje_selected"]);
		$this->assign("display_fgrad","none");
		$this->assign("display_flokops","none");
		$this->assign("display_fnaselje","none");
		switch ($fLokTip){
			case "1":
				$this->assign("display_fgrad","block");
				break;
			case "2":
				$this->assign("display_flokops","block");
				break;
			case "3":
				$this->assign("display_fnaselje","block");
				break;
		}
		if (!strlen($fUlica) && $fLokTip<1) {
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->Odaberite ulicu/lokaciju");
		}else{
			$q = "SELECT ullok.ullok_id, ullok.ullok_desc, u.*, lok.* "
				."FROM ulice_lokacije ullok, ulice u, lokacije lok "
				."WHERE ullok.ullok_ul_id=u.ulica_id "
				."AND ullok.ullok_lok_id=lok.lid ";
				
			if (strlen($fUlica)) {
				$q .= "AND u.ulica_ime LIKE '%$fUlica%' ";
			}
			if ($fLokTip>0) {
				$q .= "AND lok.lok_tip=$fLokTip ";
				switch ($fLokTip){
					case "1":
						$flid = $fGradLid;
						break;
					case "2":
						$flid = $fLokopsLid;
						break;
					case "3":
						$flid = $fNaseljeLid;
						break;
				}
				if ($flid>0) {
					$q .= "AND lok.lid=$flid ";
				}
			}
				
			$q .= "ORDER BY ulica_ime, lok_naziv";
			
			$res = $this->db->select2($q);
			if ($res !== false) {
				foreach ($res as $row){
					$ids[] = $row["ullok_id"];
					$ulids[] = $row["ulica_id"];
					$ulnames[] = $row["ulica_ime"];
					$lids[] = $row["lid"];
					$loknames[] = $row["lok_naziv"];
					$loktips[] = $this->tipovi_lokacija[$row["lok_tip"]];
					$descs[] = nl2br($row["ullok_desc"]);
				}
				$this->assign("id",$ids);
				$this->assign("ulid",$ulids);
				$this->assign("ulname",$ulnames);
				$this->assign("lokid",$lids);
				$this->assign("lokname",$loknames);
				$this->assign("loktip",$loktips);
				$this->assign("desc",$descs);
			}else{
				$this->assign("comment_start","<!--");
				$this->assign("comment_end","-->Nema rezultata.");
			}
		}
		
		echo $this->parseTemplate("geos/ulica_lokacija.tpl");
	}
	function ulice_lokacije_new(){
		$ulid	= getFromInput("new_ulica_id");
		$ulname	= readValFromInput("new_ulica_name");
		$loktip	= getFromInput("new_tip_lok");
		$desc	= readValFromInput("new_desc");
		switch ($loktip){
			case "1":
				$lid = getFromInput("new_grad");
				break;
			case "2":
				$lid = getFromInput("new_lokops");
				break;
			case "3":
				$lid = getFromInput("new_naselje");
				break;
		}
		if ($ulid>0 && $lid>0) {
			$q = "INSERT INTO ulice_lokacije(ullok_ul_id,ullok_lok_id,ullok_desc) "
				."VALUES($ulid,$lid,'".addslashes($desc)."')";
			if ($this->db->alterTable($q)) {
				echo "Unos izvršen.";
			}else {
				echo mysql_error();
			}
		}else{
			echo "Unos nije izvršen!\nNiste odabrali ulicu ili lokaciju.";
		}
		
	}
	function ulice_lokacije_delete(){
		$id = getFromInput("id");
		$q = "DELETE FROM ulice_lokacije WHERE ullok_id=$id LIMIT 1";
		if ($this->db->alterTable($q)) {
			echo "Veza je obrisana.";
		}else{
			echo mysql_error();
		}
	}
	
	function get_Parent($type,$parent){
		$type = intval($type);
		$parent = intval($parent);
		
		if($type == 1){
			$q = "SELECT opstina_name FROM opstine WHERE opstina_id=$parent LIMIT 1";
		}else{
			$q = "SELECT lok_naziv FROM lokacije WHERE lok_tip=".($type-1)." AND lid=$parent";
		}
		$ret = $this->db->getOne($q);
		return $ret;
	}
	
	
	function get_LokacijeTipovi($tip_id){
		$ret = array(
			"polje_id" => array(),
			"polje_ime" => array(),
			"polje_selected" => array()
		);
		$k=0;
		foreach ($this->tipovi_lokacija as $id=>$name){
			array_push($ret["polje_id"],$id);
			array_push($ret["polje_ime"],$name);
			if ($tip_id==$id) {
				array_push($ret["polje_selected"]," selected");
			}else{
				array_push($ret["polje_selected"],"");
			}
			$k++;
		}
		return $ret;
	}
	
}

?>